package postMan;

import okhttp3.*;

import java.io.File;
import java.io.IOException;

/**
 * ClassName MultipartTest
 * Description postMan上传文件
 * Author dzf
 * Date 2025/6/6 14:12
 * Version 1.0
 */
public class MultipartTest {
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();

        // 组装请求body
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("name","试试",
                        RequestBody.create(MediaType.parse("text/plain;charset=utf-8"),
                                new File("C:\\Users\\dzf\\Desktop\\Spring Cloud与Docker高并发微服务架构设计实施.pdf")))
                .build();

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("name","试试",
                RequestBody.create(MediaType.parse("text/plain;charset=utf-8"),
                        new File("C:\\Users\\dzf\\Desktop\\Spring Cloud与Docker高并发微服务架构设计实施.pdf")));
        RequestBody body1 = builder.build();

        // 组装请求头
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url("http://www.baidu.com");
        requestBuilder.method("POST",body1);
        requestBuilder.addHeader("content-type","multipart/form-data");
        requestBuilder.addHeader("Version","1.0.0");
        Request request = requestBuilder.build();

        // 开始请求
        try (Response response = client.newCall(request).execute()) {
            if (response.body() != null) {
                System.out.println(response.body().toString());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
            
        }
    }
}
